package com._54programer.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

@Aspect
public class Logger {

    /**
     * 切入点
     */
    @Pointcut("execution(public int com._54programer.aop.bean.Math.*(..))")
    public void pointCut(){}

    /**
     * 执行前
     */
    @Before("pointCut()")
    public void loggerBefore(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        System.out.println("loggerBefore, 参数: "+ Arrays.asList(args));
    }

    /**
     * 成功失败都走这里, 类似finally
     */
    @After("pointCut()")
    public void loggerAfter(JoinPoint joinPoint){
        System.out.println("loggerAfter, "+joinPoint.getSignature().getName());
    }

    /**
     * 执行成功时执行
     */
    @AfterReturning(value = "pointCut()", returning = "result")
    public void loggerReturn(JoinPoint joinPoint,Object result){
        System.out.println(""+joinPoint.getSignature().getName()+"正常返回。。。@AfterReturning:运行结果：{"+result+"}");
    }

    /**
     * 发生异常的时候执行这里
     */
    @AfterThrowing(value="pointCut()",throwing="exception")
    public void loggerException(JoinPoint joinPoint,Exception exception){
        System.out.println(""+joinPoint.getSignature().getName()+"异常。。。异常信息：{"+exception+"}");
    }

}
